// pages/list-page/academy-list/academy-list.js
let self
var app = getApp();
const { FILTERS } = require('../../config/filters.js');
const { mayjorList, schoolList, rulesList, addressList } = require('../../config/api/index.js');
Page({

  /**
   * 页面的初始数据
   */
	data: {
		isloading: true,
		moreCoursesLoading: false,  // 加载更多课程的状态
		listData: [],
		filters: [],
		currentFilter: [],  // 当前选择的筛选
		//general: false,  // 是否为招生简章页面
		menuState: true,  // 菜单状态
		currentMenu: {},
		openState: false,  // 列表按钮菜单显示状态
		maskState: false,
		pageType: 'academy',  // 页面属性
		curPage: 1,  // 当前页
		perPage: 10,  // 每页的数量
		noMoreState: false,  // 没有更多数据
		filterText: '',  // 筛选或者搜索的文本wxRequest
		filterPage: 1,
		filterType: '',  // 筛选类型
		addressPage: 1,
		mayjorPage: 1,
		school: [],  // 全部学校
		address: [],  // 全部地区
		mayjor: [],  // 全部专业
		area: '',  // 地区id
		project: '',  // 专业id
		schoolid: '',  // 学校id
		class_type: '',  // 班型id
		school_type: '',
		onlyfilter: []
	},
  /**
   * 生命周期函数--监听页面加载
   */
	onLoad: function (options) {
		self = this;
	
		
		wx.showLoading({
			title: '加载中...',
			mask: true
		})
		let filters = FILTERS[options.filters] || FILTERS['academy']  // 获取对应列表页静态数据

		let currentFilter = {
			area: self.data.area || options.area || '',
			project: self.data.project || '',
			school_type: self.data.school_type || '',
			class_type: self.data.class_type || '',
		}
		self.setData({
			currentFilter
		})
		if (options.filters=='banner'){  // 是否从banner跳转
			self.getSchoolList(currentFilter)
			self.setData({
				onlyfilter: [
					{ address: '407', index: '0', name: '北京', filtertype:'address' }
				]
			})
		}
		self.setData({
			filters: filters.data,
			filterText: options.searchVal || '',
			area: options.area || ''
		})
		// self.setData({
		// 	general: false
		// })

		if (self.data.pageType == 'academy') {  // 调取学校列表
			self.getSchoolList()
			self.getAddressList()
		}
	},

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
	onReady: function () {

	},
	getSchoolList(params, extend) { // 获取学校列表
		const { perPage, listData } = self.data;
		return schoolList({
			data: {
				...{
					per_page: perPage,
					is_get: 1
				},
				...params
			},
			success(res) {
				let totalPages = res.data.total_pages
				if (extend) {
					let curPage = Math.ceil((listData.length + res.data.items.length) / perPage);
					self.setData({
						listData: [...listData, ...res.data.items],
						curPage,
						noMoreState: parseInt(totalPages) <= curPage
					})
				} else {
					self.setData({
						listData: res.data.items,
						curPage: 1,
						noMoreState: parseInt(totalPages) <= Math.ceil(res.data.items.length / perPage)
					})
				}
			},
			complete(res) {
				self.setData({
					isloading: false,
					moreCoursesLoading: false,
				})
			}

		})

	},
	getRulesList(params, extend) {  // 获取简章列表
		const { perPage, listData } = self.data;
		return rulesList({
			data: {
				...{
					per_page: perPage,
				},
				...params
			},
			success(res) {
				let totalPages = res.data.total_pages
				if (extend) {
					let curPage = Math.ceil((listData.length + res.data.items.length) / perPage);
					self.setData({
						listData: [...listData, ...res.data.items],
						curPage,
						noMoreState: parseInt(totalPages) <= curPage
					})
				} else {
					self.setData({
						listData: res.data.items,
						curPage: 1,
						noMoreState: parseInt(totalPages) <= Math.ceil(res.data.items.length / perPage)
					})
				}
			},
			complete(res) {
				self.setData({
					isloading: false,
					moreCoursesLoading: false,
				})
			}

		})
	},
	getAddressList(param) {  // 获取全部地区列表
		const { perPage, addressPage, address } = self.data;
		return addressList({
			data: { per_page: 100, ...param },
			success(res) {
				let totalPages = res.data.total_pages
				if (addressPage <= totalPages) {
					self.setData({
						addressPage: addressPage + 1,
						address: [...address, ...res.data.items]
					})
					setTimeout(function () {
						self.getAddressList({ page: addressPage + 1 })
					}, 0)
				} else {
					let data = self.data.address
					let address = [];
					for (let i = 0; i < data.length; i++) {
						if (data[i].level === '2') {
							address.push({
								typeid: data[i].id,
								val: data[i].name,
								lists: []
							})
						}
					}
					let filters = self.data.filters
					let index = 2
					if (self.data.pageType == 'academy') {
						index = 0
					}
					filters[index].lists = address
					self.setData({
						filters
					})
				}
			}
		})


	},
	getSchoolFilter(params) {  // 获取全部学校列表
		const { perPage, listData, filterPage, school } = self.data;

		return schoolList({
			data: {
				...{
					per_page: perPage,
					is_get: 1
				},
				...params
			},
			success(res) {

				let totalPages = res.data.total_pages
				if (filterPage <= totalPages) {
					self.setData({
						filterPage: filterPage + 1,
						school: [...school, ...res.data.items]
					})

					setTimeout(function () {
						self.getSchoolFilter({ page: filterPage + 1 })
					}, 0)
				} else {
					let filters = self.data.filters

					filters[0].lists = school
					self.setData({
						filters
					})
				}

			}
		})
	},
	onShow: function (e) {
	
	},
	onmask(e) {
		this.setData({
			maskState: false
		})
	},
	onMenuDown(e) {
		self.setData({
			filterType: e.detail,
		})
	},
	onCurrent(e) {
		self.setData({
			listData: [],
			onlyfilter: [],
			filterText: '',
			isloading: false
		})
		let category = [];
		wx.showLoading({
			title: '加载中...',
		})

		for (let k in e.detail) {
			category = [];
			category.push(e.detail)
		}
		self.setData({
			area: category[0]['address'] || '',
			project: category[0]['bkType'] || '',
			school_type: category[0]['schoolType'] || '',
			class_type: category[0]['classType'] || '',
		})

		let currentFilter = {
			area: self.data.area || category[0]['address'] || '',
			project: self.data.project || category[0]['bkType'] || '',
			school_type: self.data.school_type || category[0]['schoolType'] || '',
			class_type: category[0]['classType'] || '',
		}
		self.setData({
			currentFilter
		})
		for (let key in currentFilter) {              // 去除对象内多余的空值key  
			if (currentFilter[key] === '') {
				delete currentFilter[key]
			}
		}
		self.getSchoolList(currentFilter)
	},

	showList: function (e) {
		let filtertit = e.detail['filtertit']
		let id = e.detail['id']
		wx.navigateTo({
			url: '/pages/list-page/list-page?filters=school&title=' + filtertit + '&id=' + id,
		})
	},
	makePhoneCall: function () { // 拨号
		app.makePhoneCall()
		self.setData({
			openState: true
		})
	},
	sign: function (e) {
		let schoolName = e.detail['filtertit']
		let schoolId = e.detail['id']
		wx.navigateTo({
			url: '/pages/form-page/sign-up/sign-up?filters=schoollist&schoolName=' + schoolName + '&schoolId=' + schoolId,
		})
		self.setData({
			openState: true
		})
	},
	searchCourses(e) {
		// 执行搜素
		self.setData({
			listData: [],
			isloading: false
		})
		wx.showLoading({
			title: '加载中...',
			mask: true
		})
		setTimeout(function () {
			wx.hideLoading()
		}, 2000)
		self.setData({
			filterText: e.detail,
			isloading: false
		})
		
		self.getSchoolList({
			area: '',
			project: '',
			school: '',
			class_type: '',
			search: self.data.filterText || '',
		}, true)
		
	},
	blockCatch: function () {
		//阻断外层点击时间进入客服消息
		self.setData({
			openState: true
		})
	},
	onShareAppMessage: function (res) {
		return {
			title: '在职研究生网找学校',
			path: '/pages/schoollist/schoollist'
		}
	},
	onReachBottom() {
		// 加载更多
		if (self.data.noMoreState) {
			return false;
		}
		self.setData({
			moreCoursesLoading: true,
		})
		
		let currentFilter = self.data.currentFilter;
		for (let key in currentFilter) {              // 去除对象内多余的空值key  
			if (currentFilter[key] === '') {
				delete currentFilter[key]
			}
		}
		self.getSchoolList({
			page: self.data.curPage + 1, 
			...currentFilter
		}, true)
		
	}
})